{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Truncate Sentence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: truncateSentence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #截断句子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><strong>句子</strong> 是一个单词列表，列表中的单词之间用单个空格隔开，且不存在前导或尾随空格。每个单词仅由大小写英文字母组成（不含标点符号）。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，<code>\"Hello World\"</code>、<code>\"HELLO\"</code> 和 <code>\"hello world hello world\"</code> 都是句子。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个句子 <code>s</code>​​​​​​ 和一个整数 <code>k</code>​​​​​​ ，请你将 <code>s</code>​​ <strong>截断</strong> ​，​​​使截断后的句子仅含 <strong>前</strong> <code>k</code>​​​​​​ 个单词。返回 <strong>截断</strong> <code>s</code>​​​​<em>​​ </em>后得到的句子<em>。</em></p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"Hello how are you Contestant\", k = 4\n",
    "<strong>输出：</strong>\"Hello how are you\"\n",
    "<strong>解释：</strong>\n",
    "s 中的单词为 [\"Hello\", \"how\" \"are\", \"you\", \"Contestant\"]\n",
    "前 4 个单词为 [\"Hello\", \"how\", \"are\", \"you\"]\n",
    "因此，应当返回 \"Hello how are you\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"What is the solution to this problem\", k = 4\n",
    "<strong>输出：</strong>\"What is the solution\"\n",
    "<strong>解释：</strong>\n",
    "s 中的单词为 [\"What\", \"is\" \"the\", \"solution\", \"to\", \"this\", \"problem\"]\n",
    "前 4 个单词为 [\"What\", \"is\", \"the\", \"solution\"]\n",
    "因此，应当返回 \"What is the solution\"</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"chopper is not a tanuki\", k = 5\n",
    "<strong>输出：</strong>\"chopper is not a tanuki\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 500</code></li>\n",
    "\t<li><code>k</code> 的取值范围是 <code>[1,  s 中单词的数目]</code></li>\n",
    "\t<li><code>s</code> 仅由大小写英文字母和空格组成</li>\n",
    "\t<li><code>s</code> 中的单词之间由单个空格隔开</li>\n",
    "\t<li>不存在前导或尾随空格</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [truncate-sentence](https://leetcode.cn/problems/truncate-sentence/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [truncate-sentence](https://leetcode.cn/problems/truncate-sentence/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"Hello how are you Contestant\"\\n4', '\"What is the solution to this problem\"\\n4', '\"chopper is not a tanuki\"\\n5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        words = s.split(' ')\n",
    "        ans = ''\n",
    "        for i in range(k):\n",
    "            if i != k - 1:\n",
    "                ans += words[i] + ' '\n",
    "            else:\n",
    "                ans += words[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        # start=-1\n",
    "        # a=\"\"\n",
    "  \n",
    "        # for index,i in enumerate(s):\n",
    "        #     if k==0:\n",
    "        #         break\n",
    "        #     if i ==\" \":\n",
    "        #         k=k-1\n",
    "        #         a=a+s[start+1:index]\n",
    "        #         if k!=0:\n",
    "        #             a=a+\" \"\n",
    "        #         start=index\n",
    "        #     if  index==len(s)-1:\n",
    "        #         a=a+s[start+1:index+1]\n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "        # return a\n",
    "        a=\"\"\n",
    "        for i in s:\n",
    "            if k==0:\n",
    "                break\n",
    "            if i==\" \":\n",
    "                k=k-1\n",
    "                if k!=0:\n",
    "                    a=a+\" \"\n",
    "                \n",
    "            else:\n",
    "                a=a+i\n",
    "        return a\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        a=s.split()\n",
    "        b=[]\n",
    "        for i in range(k):\n",
    "            b.append(a[i])\n",
    "        return ' '.join(b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        s = s.split()\n",
    "        ans = [s[i] for i in range(k)]\n",
    "        return \" \".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        a = s.split(' ')\n",
    "        return ' '.join(a[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        idx = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if c ==' ':\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                idx = i\n",
    "                break\n",
    "        if k == 0:\n",
    "            return s[:i]\n",
    "        else:\n",
    "            return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        s=s.split()[:k]\n",
    "        return ' '.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split()[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        ans = []\n",
    "        s = s.split(' ')\n",
    "        for i in range(k):\n",
    "            ans.append(s[i])\n",
    "\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(\" \")[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        t=s.split(' ')\n",
    "        res=t[:k]\n",
    "        ans=''\n",
    "        for i in range(k-1):\n",
    "            ans+=t[i]+' '\n",
    "        ans+=t[k-1]\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        # li  = [i for i in s.split(' ')]\n",
    "        # return ' '.join(li[:k])\n",
    "        idx = None\n",
    "        for i, c in enumerate(s):\n",
    "            if c == \" \":\n",
    "                k -= 1\n",
    "            if not k:\n",
    "                idx = i\n",
    "                break\n",
    "        return s[:idx] if idx is not None else s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        t=s.split(' ')\n",
    "        return ' '.join(t[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        \n",
    "        for i in range(len(s)):\n",
    "            if s[i]==' ':\n",
    "                k-=1\n",
    "                if k==0:\n",
    "                    return s[:i]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        count=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \" \":\n",
    "                count=count+1\n",
    "            if count == k:\n",
    "                s = s[0:i]\n",
    "                break  \n",
    "        return s  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        ss = s.split()\n",
    "        return ' '.join(ss[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        s=s.split()\n",
    "        return (' '.join (s[:k]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join((s.split())[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        nums=s.split(' ')\n",
    "        return ' '.join(nums[:k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        str_list=list(s.split())\n",
    "        result=\"\"\n",
    "        for i in range(k):\n",
    "            if i<=k-2:\n",
    "                result+=str_list[i]+' '\n",
    "            else:\n",
    "                result+=str_list[i]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        count_empty = 0\n",
    "        return_num = 0\n",
    "        for i in range(0,len(s)):\n",
    "            if s[i] ==' ':\n",
    "                count_empty += 1\n",
    "                if count_empty == k:\n",
    "                    return_num = i\n",
    "        if count_empty < k:\n",
    "            return s\n",
    "        else:\n",
    "            return s[:return_num]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        s = s.split(' ')[:k]\n",
    "        return ' '.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s, k):\n",
    "        return ' '.join(s.split(' ')[:k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        list_s = s.split()\n",
    "        new_s = ' '.join(list_s[0 : k])\n",
    "\n",
    "        return new_s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s, k):\n",
    "        lst = s.split(' ')[0:k]\n",
    "        a = lst[0]\n",
    "        for i in lst[1:k]:\n",
    "            a = a + ' ' + i\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split()[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        j=0\n",
    "        ans=\"\"\n",
    "        for i in s:\n",
    "            if i==\" \":\n",
    "                j+=1\n",
    "            if j==k:\n",
    "                break\n",
    "            ans=ans+i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        list1 = s.split(' ')\n",
    "        return ' '.join(list1[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join( s.split(' ')[:k] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split()[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(\" \")[:k]).strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        words = s.split()\n",
    "        return ' '.join(words[0:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        s=s.split()\n",
    "        count=0\n",
    "        return (' '.join (s[:k]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        ans = []\n",
    "\n",
    "        word = 0\n",
    "        for i in range (n):\n",
    "           \n",
    "            if s[i] == ' ':\n",
    "                word += 1\n",
    "                \n",
    "            if word == k:\n",
    "                ans = s[0:i]\n",
    "                break\n",
    "            ans = s\n",
    "                \n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(\" \")[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "         return \" \".join(s.split(\" \")[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        n, end, count = len(s),0,0\n",
    "        for i in range(1,n+1):\n",
    "            if i==n or s[i]==' ':\n",
    "                count+=1\n",
    "                if count == k:\n",
    "                    end = i\n",
    "                    break\n",
    "        return s[:end]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        arr = s.split(\" \")\n",
    "        sentence = \"\"\n",
    "        for i in range(k):\n",
    "            sentence += arr[i]\n",
    "            sentence += \" \"\n",
    "\n",
    "        return sentence[:len(sentence) - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        s_lst = s.split()\n",
    "        return ' '.join(s_lst[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(\" \")[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(' ')[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(\" \")[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        l = len(s)\n",
    "        for i in range(1, l):\n",
    "            v = s[i]\n",
    "            if ' ' == v:\n",
    "                k -= 1\n",
    "                if 0 == k:\n",
    "                    return s[:i]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split()[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        ant=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]==' ':\n",
    "                ant+=1\n",
    "                if ant==k:\n",
    "                    return s[:i]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        words=s.split(' ')\n",
    "        print()\n",
    "        st=words[:k]\n",
    "        return ' '.join(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split(' ')[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        nums=s.split(' ')\n",
    "        ss=''\n",
    "        for i in range(k-1):\n",
    "            ss+= (nums[i]+' ')\n",
    "        ss+=nums[k-1]\n",
    "        return ss\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        l1 = list(s.split(\" \"))\n",
    "        x = \"\"\n",
    "        for i in range (k - 1):\n",
    "            x += l1[i] + \" \"\n",
    "        return x + l1[k - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        # start=-1\n",
    "        # a=\"\"\n",
    "  \n",
    "        # for index,i in enumerate(s):\n",
    "        #     if k==0:\n",
    "        #         break\n",
    "        #     if i ==\" \":\n",
    "        #         k=k-1\n",
    "        #         a=a+s[start+1:index]\n",
    "        #         if k!=0:\n",
    "        #             a=a+\" \"\n",
    "        #         start=index\n",
    "        #     if  index==len(s)-1:\n",
    "        #         a=a+s[start+1:index+1]\n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "        # return a\n",
    "        \n",
    "\n",
    "        # a=\"\"\n",
    "        # for i in s:\n",
    "        #     if k==0:\n",
    "        #         break\n",
    "        #     if i==\" \":\n",
    "        #         k=k-1\n",
    "        #         if k!=0:\n",
    "        #             a=a+\" \"\n",
    "                \n",
    "        #     else:\n",
    "        #         a=a+i\n",
    "        # return a\n",
    "        \n",
    "        for i in range(0,len(s)):\n",
    "            if k==0:\n",
    "                break\n",
    "            if s[i]==\" \":\n",
    "                k=k-1\n",
    "                end=i\n",
    "            if i==len(s)-1:\n",
    "                end=i+1\n",
    "        return s[:end]\n",
    "\n",
    "            \n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        a = s.split(\" \")\n",
    "        return ' '.join(a[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split()[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \" \".join(s.split(\" \")[:k])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return \"\".join(i + \" \" for i in (s.split())[:k])[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        count=1\n",
    "        words=s.split(' ')\n",
    "        ans=[]\n",
    "        for i in words:\n",
    "            if count<=k:\n",
    "                ans.append(i)\n",
    "                count+=1\n",
    "        return ' '.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def truncateSentence(self, s: str, k: int) -> str:\n",
    "        return ' '.join(s.split()[0:k])"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
